package com.example.easyexcel.controller;

import com.alibaba.fastjson.JSON;
import com.example.easyexcel.enums.UserStatusEnum;
import com.example.easyexcel.handler.SpinnerWriterHandler;
import com.example.easyexcel.listener.ExcelListener;
import com.example.easyexcel.model.Address;
import com.example.easyexcel.model.DropdownModel;
import com.example.easyexcel.model.UserModel;
import com.example.easyexcel.strategy.CustomCellStyleStrategy;
import com.example.easyexcel.strategy.CustomColumnWidthStrategy;

import java.util.*;

import static com.alibaba.excel.EasyExcelFactory.read;
import static com.alibaba.excel.EasyExcelFactory.write;

/**
 * @author dangwen1
 * @date 2021年12月02日 20:55
 * @description
 */
//@Controller
public class ExcelController {
    public static void main(String[] args) {
//        writeExcelWithDropdown2();
//        writeExcelWithDropdown();
        writeExcel();
    }

    public static void writeExcel(){
        List<UserModel> userModels = getUserModels();
        String fileName = "D:\\dangwen\\excel-test.xlsx";
        write(fileName, UserModel.class).sheet().head(UserModel.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(userModels);
    }

    public static void readExcel(){
        String fileName = "D:\\dangwen\\excel-test.xlsx";
        ExcelListener<UserModel> excelListener = new ExcelListener<>();
        read(fileName, UserModel.class, excelListener).sheet().doRead();
        List<UserModel> dataList = excelListener.getDataList();
        System.out.println(JSON.toJSONString(dataList));
    }

    public static void writeExcelWithDropdown(){
        List<UserModel> userModels = getUserModels();
        String fileName = "D:\\dangwen\\excel-test.xlsx";
        List<Integer> ageList = getAgeList();
        write(fileName, UserModel.class).sheet().head(UserModel.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy())
                .registerWriteHandler(new SpinnerWriterHandler(ageList))
                .doWrite(userModels);
    }

    public static void writeExcelWithDropdown2(){
        String fileName = "D:\\dangwen\\excel-test2.xlsx";
        List<Integer> ageList = getAgeList();
        write(fileName, DropdownModel.class).sheet().head(DropdownModel.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy())
                .registerWriteHandler(new SpinnerWriterHandler(ageList))
                .doWrite(new ArrayList<DropdownModel>());
    }

    private static List<UserModel> getUserModels() {
        List<UserModel> userModels = new ArrayList<>();
        UserModel userModel = new UserModel();
        userModel.setUsername("党文");
        userModel.setAge(18);
        userModel.setUserStatusEnum(UserStatusEnum.EFFECT);
        List<Address> addressList = new ArrayList<>();
        Address address = new Address();
        address.setProvinceId(1);
        address.setCityId(1);
        address.setProvinceName("河南省");
        address.setCityName("郑州市");
        Address address1 = new Address();
        address1.setProvinceId(2);
        address1.setCityId(2);
        address1.setProvinceName("吉林省");
        address1.setCityName("长春市");
        addressList.add(address);
        addressList.add(address1);
        userModel.setAddresses(addressList);
        userModels.add(userModel);
        return userModels;
    }

    private static List<Integer> getAgeList() {
        List<Integer> ageList = new ArrayList<>();
        ageList.add(18);
        ageList.add(19);
        ageList.add(20);
        ageList.add(21);
        ageList.add(22);
        ageList.add(23);
        ageList.add(24);
        ageList.add(25);
        return ageList;
    }
}
